package com.coachtam.ljgcapi.utils;

import com.coachtam.ljgcapi.vo.ResultVO;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpResponse;
import reactor.core.publisher.Mono;

import java.nio.charset.StandardCharsets;

/**
 * @Copyright (C), 2018-2020
 * @Author: JAVA在召唤
 * @Date: 2020-08-05 16:33
 * @Description:
 */
@Slf4j
public class ResponseUtils {

    public static Mono<Void> printJson(ServerHttpResponse response, ResultVO resultVO) {
        ObjectMapper objectMapper = new ObjectMapper();
        Mono<Void> result = null;
        try {
            response.setStatusCode(HttpStatus.TOO_MANY_REQUESTS);
            byte[] bytes = objectMapper.writeValueAsString(resultVO).getBytes(StandardCharsets.UTF_8);
            DataBuffer buffer = response.bufferFactory().wrap(bytes);
            result =  response.writeWith(Mono.just(buffer));
        } catch (JsonProcessingException e) {
            log.error("打印json出错",e);
        }
        return result;
    }

}
